package com.hkfrich.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkfrich.common.constant.Constants;
import com.hkfrich.common.utils.SecurityUtils;
import com.hkfrich.common.utils.StringUtils;
import com.hkfrich.system.domain.SysUser;
import com.hkfrich.system.security.domain.SecurityUserDetails;
import com.hkfrich.system.service.SysMenuService;
import com.hkfrich.system.service.SysRoleMenuService;
import com.hkfrich.system.service.SysUserService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import com.hkfrich.common.controller.BaseController;
import com.hkfrich.common.utils.ConvertUtils;
import com.hkfrich.common.utils.ResultUtils;
import com.hkfrich.system.domain.SysRole;
import com.hkfrich.system.service.SysRoleService;
//import com.hkfrich.common.core.page.TableDataInfo;

/**
 * @ClassName 角色Controller
 * @Desscription
 * @Author _Heishuai
 * @Date 2022-09-24 00:21:07
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;


    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 通过条件查询单个角色 不分页
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public ResultUtils getOne(SysRole sysRole)
    {
        QueryWrapper<SysRole> query = initBaseQueryWrapper(sysRole);
        SysRole one = sysRoleService.getOne(query);
        return ResultUtils.success(one,"角色查询成功");
    }

    /**
     * 通过id查询单个角色
     */
    @RequestMapping("/getOneById")
    @ResponseBody
    public ResultUtils getOneById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        SysRole one = sysRoleService.getById(id);
        return ResultUtils.success(one,"角色查询成功");
    }

    /**
     * 查询角色列表  分页
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultUtils list(@RequestBody SysRole sysRole)
    {
        Page<SysRole> page = initBaseQueryPageWrapper(sysRole);
        QueryWrapper<SysRole> query = initBaseQueryWrapper(sysRole);
        query.orderByAsc(Constants.ROLE_SORT_COLUM).ne(Constants.DEL_FLAG_COLUMN,Constants.DEL_FLAG_VALUE);
        Page<SysRole> pageGrid = sysRoleService.page(page, query);
        return ResultUtils.success(pageGrid,"角色列表查询成功");
    }

    /**
     * 修改启用状态
     */
    @RequestMapping("/changeRoleStatus")
    @ResponseBody
    public ResultUtils changeRoleStatus(@RequestBody SysRole sysRole) {
        return ResultUtils.success(sysRoleService.updateById(sysRole),Constants.STATUS_DISABLE.equals(sysRole.getStatus())?"停用成功":"启用成功");
    }

    /**
     * 新增角色
     */
    @RequestMapping("/save")
    @ResponseBody
    @Transactional
    public ResultUtils save(@RequestBody SysRole sysRole)
    {
        if (Constants.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(sysRole)))
        {
            return ResultUtils.fail("新增角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }
        else if (Constants.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(sysRole)))
        {
            return ResultUtils.fail("新增角色'" + sysRole.getRoleName() + "'失败，角色权限已存在");
        }
        sysRole.setCreateBy(SecurityUtils.getUserId());
        sysRoleService.save(sysRole);
        sysRoleMenuService.insertRoleMenu(sysRole);
        return ResultUtils.success("角色保存成功");
    }

    /**
     * 修改角色
     */
    @RequestMapping("/updateById")
    @ResponseBody
    public ResultUtils updateById(@RequestBody SysRole sysRole)
    {
        if (Constants.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(sysRole)))
        {
            return ResultUtils.fail("修改角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }
        else if (Constants.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(sysRole)))
        {
            return ResultUtils.fail("修改角色'" + sysRole.getRoleName() + "'失败，角色权限已存在");
        }
        sysRole.setUpdateBy(SecurityUtils.getUserId());
        if (sysRoleService.updateRole(sysRole) > 0)
        {
            // 更新缓存用户权限
            SecurityUserDetails securityUserDetail = SecurityUtils.getSecurityUserDetail();
            if (StringUtils.isNotNull(securityUserDetail.getSysUser()) && !securityUserDetail.getSysUser().isAdmin())
            {
                securityUserDetail.setSysUser(sysUserService.selectUserVoByUserName(securityUserDetail.getSysUser().getUserName()));
            }
            return ResultUtils.success("修改权限成功。");
        }
        return ResultUtils.fail("修改角色'" + sysRole.getRoleName() + "'失败，请联系管理员");
    }

    /**
     * 删除角色
     */
    @RequestMapping("/removeById")
    @ResponseBody
    public ResultUtils removeById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        return ResultUtils.success(sysRoleService.removeById(id),"角色删除成功");
    }

    /**
     * 批量删除角色
     */
    @RequestMapping("/removeByIds")
    @ResponseBody
    public ResultUtils removeByIds(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        String ids = params.getString("ids");
        List<Long> list = ConvertUtils.toList(ids);
        return ResultUtils.success(sysRoleService.deleteRoleByIds(list),"批量删除角色成功");
    }

}
